/*
-----------------------------------------------------------------------------
This source file is part of ogre-procedural

For the latest info, see http://code.google.com/p/ogre-procedural/

Copyright (c) 2010 Michael Broutin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


-----------------------------------------------------------------------------
*/

//test svn
#ifndef PROCEDURAL_TUNNEL_GENERATOR_INCLUDED
#define PROCEDURAL_TUNNEL_GENERATOR_INCLUDED

#include "Ogre.h"
#include "ProceduralMeshGenerator.h"
#include "ProceduralPlatform.h"
#include "MovableText.h"
namespace Procedural
{

class _ProceduralExport TunnelGenerator : public MeshGenerator<TunnelGenerator>
{
    int numSegBase;
    int numSegLength;
	int numSegHeight;
    bool capped;
    float radius;
    float length;
	float height;
	Ogre::Vector3 directionVector;// direction for the start and end point
	Ogre::Matrix4 translateMatrix4;
	std::vector<Ogre::Vector3> pointVector;

public:
    TunnelGenerator() : numSegBase(16),
        numSegLength(8),
		numSegHeight(4),
        capped(true),
        radius(1.f),
        length(8.f),
		height(0.5)
    {}
	TunnelGenerator(Ogre::SceneManager* mSceneMgr,float radius=0.5,float height=0.5,bool capped=true):
		 numSegBase(16),
        numSegLength(8),
		numSegHeight(4),
        capped(true),
        radius(1.f),
        length(8.f),
		height(0.5)
	 {
		 Procedural::Root::getInstance()->sceneManager = mSceneMgr;
		 sceneMgr=mSceneMgr;

	 }

	 void addpoint(Ogre::Vector3 point)
	 {
		 pointVector.push_back(point);
	 }
	 
	 void close()
	 {
		 assert(pointVector.size()>=2&&"The number of the points should be more than one");
		 for(std::vector<Ogre::Vector3>::iterator i=pointVector.begin();(i+1)!=pointVector.end();i++)
		 {
			 Ogre::StringConverter *strConverter;
			 Ogre::String meshName="meshPosition";
			 meshName+=strConverter->toString((*i).x);
			 meshName+=","+strConverter->toString((*i).y);
			 meshName+=","+strConverter->toString((*i).z);
			 
			 createTunnel(meshName,*i,*(i+1));

			 Ogre::String textString;
			 textString="("+strConverter->toString((*i).x);
			 textString+=","+strConverter->toString((*i).y);
			 textString+=","+strConverter->toString((*i).z)+")";
			// Ogre::SceneNode *sn=sceneMgr->getRootSceneNode()->createChildSceneNode(textString);
			// MovableText *msg=new MovableText(textString,textString);
			// sn->attachObject(msg);
			// sn->setPosition(*i);

		 }

	 }


    void addToManualObject(Ogre::ManualObject* manual, int& offset, float& boundingRadius, Ogre::AxisAlignedBox& aabb);

    inline TunnelGenerator & setNumSegBase(int numSegBase)
    {
        this->numSegBase = numSegBase;
        return *this;
    }

    inline TunnelGenerator & setNumSegLength(int numSegLength)
    {
        this->numSegLength = numSegLength;
        return *this;
    }

    inline TunnelGenerator & setCapped(bool capped)
    {
        this->capped = capped;
        return *this;
    }

    inline TunnelGenerator & setRadius(float radius)
    {
        this->radius = radius;
        return *this;
    }

    inline TunnelGenerator & setLength(float length)
    {
        this->length = length;
        return *this;
    }
	inline TunnelGenerator & setHeight(float height)
    {
        this->height = height;
        return *this;
    }



	
	/*
	a function that can creat a box from start point to end point 
	*/

	TunnelGenerator& setStartEndPoint(Ogre::Vector3 startpoint,Ogre::Vector3 endpoint,Ogre::Real height,Ogre::Real radius)
	{
		const Ogre::Real fx=-startpoint.x+endpoint.x;
		const Ogre::Real fy=-startpoint.y+endpoint.y;
		const Ogre::Real fz=-startpoint.z+endpoint.z;
		
		//setSizeX(fx).setSizeY(fy).setSizeZ(fz);
		directionVector=Ogre::Vector3(fx,fy,fz);
		

		const Ogre::Real mLength=directionVector.length();
	
		return setLength(mLength).setHeight(height).setRadius(radius);
	}


	Ogre::SceneNode* putMesh(const std::string& meshName, const Ogre::Vector3  position,const std::string& materiaName="Examples/Rockwall")
	{
		Ogre::Entity* ent2 = sceneMgr->createEntity(meshName);
		Ogre::SceneNode* sn = sceneMgr->getRootSceneNode()->createChildSceneNode();
		sn->attachObject(ent2);
		sn->setPosition(position);
		ent2->setMaterialName(materiaName);
		ent2->setCastShadows(false);
		return sn;

	}
	Ogre::SceneNode  *createTunnel(const std::string &meshName,Ogre::Vector3 startpoint,Ogre::Vector3 endpoint,Ogre::Real height=0.5,Ogre::Real radius=0.5,bool capped=true,const std::string& materiaName="Examples/Rockwall")
	{
		TunnelGenerator::setStartEndPoint(startpoint,endpoint,height,radius).setCapped(capped).realizeMesh(meshName);
		Ogre::SceneNode *sceneNode=putMesh(meshName,Ogre::Vector3(0,0,0),materiaName);	
		Ogre::Vector3 directionNormal=directionVector.normalisedCopy();
		Ogre::Quaternion rotate=Ogre::Vector3::UNIT_Y.getRotationTo(directionNormal);
		sceneNode->rotate(rotate);
		sceneNode->yaw(Ogre::Degree(-90),Ogre::Node::TS_LOCAL);
		
		sceneNode->setPosition(startpoint);
		return sceneNode;
	}


};

}

#endif
